<?php
class Article_model extends MY_Model 
{
    protected $tab_one = 'posts';
    protected $tab_tow = 'terms';
    protected $tab_three = 'term_relationships';
    protected $tab_for = 'users';
    protected $tab_com = 'comments';
    protected  $classID = '';

    public  function __construct() {
		parent::__construct();
       $this->load->database();
       $this->load->library('pagination');
       $this->load->helper('url');
    }
    /*
    *获取根分类目录
    */
   public function get_class($article)
   {    
        switch($article){
            case 1:
                    $this->db->where('name' ,'健康');
                    $result = $this->db->get($this->tab_tow)->row_array();
                    //$this->classID = $result['term_id'];
                    return $result; 
                    break;
            case 2:
                    $this->db->where('name' ,'癌症');
                    $result = $this->db->get($this->tab_tow)->row_array();
                    //$this->classID = $result['term_id'];
                    return $result;
                    break;
            case 3:
                    $this->db->where('name' ,'糖尿病');
                    $result = $this->db->get($this->tab_tow)->row_array();
                    //$this->classID = $result['term_id'];
                    return $result; 
                    break;
        }

   }
   
    /*此处记得要优化，用join查询！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
    
   *在文章的分类的关连表中以根类ID找出所有它的子类ID
   */
   public function get_article($ID)
   {
        $this->db->where('term_taxonomy_id', $ID);//评论表的此ID只要等于分类ID，它就是此分类的子类
        $result = $this->db->get($this->tab_three)->result_array();
        
        $articleId = "";
        foreach($result as $vl){
            $articleId .= $vl['object_id'].',';
        }
        return  rtrim($articleId, ',');
   }
   
   /*
   *取的符合条件的文章列表
   */
   public function get_article_list($where,$classdir,$classId,$ppage)
   {
        $total=$this->get_total($where);
        $pagearr=$this->page($classId, $ppage, $total);
        $num = $pagearr['num'];
        $start = $pagearr['start'];
        $arr = explode(',', $where);
        $this->db->where_in('ID', $arr);
        $this->db->where('post_content !=', "");
        
        $this->db->limit($num ,$start);
        $result = $this->db->get($this->tab_one)->result_array();
        foreach($result as &$v){
            $sql = "select user_login from ao_users where ID = {$v['post_author']}";
            $res = $this->db->query($sql);
             $name = $res ->row_array();
            $v['pubadmin'] = $name['user_login'];
            preg_match('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',$v['post_content'],$match);
            $v['img']=$match[0];
            $v['classdir']=$classdir;
        }
            
            
            $result['page'] = $pagearr['pa'];
            $result['classdir'] = $classdir;
            return $result;
   }
   
  
   private function get_total($where)
   {
    
            $sql = "select * from ao_posts where ID in({$where})";
            $query = $this->db->query($sql);
            $total = $query->num_rows(); 
            return $total;
   }
   
   /*
   *取出单个文章信息
   */
   public function get_article_content($ID)
   {
        $sql = "select A.post_title, A.post_date, A.post_content , U.user_login from ao_posts  A  join ao_users  U on  A.post_author = U.ID where A.ID = {$ID}";
        $res = $this ->db->query($sql);
        $row = $res->row_array();
        return $row;
   }
   
   /*
   *取出文章的所有评论
   */
   public function get_article_comments($ID)
   {
        $this->db->where('comment_post_ID', $ID);
        $this->db->order_by("comment_ID", "desc"); 
        $result = $this->db->get($this->tab_com)->result_array();
        foreach($result as &$v){
            $sqll = "select * from ao_comments where comment_parent = {$v['comment_ID']}";
            $res = $this->db->query($sqll);
            $re = $res->result_array();
            $v['tow_comment'] = $re;
        }
        
        $count = count($result);
        $result['count'] = $count;
        
        return $result;
        
        //$sql = "select comment_parent comment_ID, comment_post_ID, comment_author,user_id,comment_author_IP,comment_author_email, //comment_content, comment_date from ao_comments  where  comment_post_ID = {$ID}";
        //$resu = $this->db->query($sql);
        
        //$result = $resu->result_array();
        //$count = count($result);
        //$result['count'] = $count;
        //return $result;
   }
   
   /*
   *评论信息录入
   */
   public function insert_comment($comment_authorInfo)
   {
        $comment_authArr = json_decode($comment_authorInfo, true);
        $articleID=$comment_authArr['articleid'];
        $comment_author=$comment_authArr['username'];
        $userID=$comment_authArr['userid'];
        $login_ip=$comment_authArr['userip'];
        $user_email=$comment_authArr['useremail'];
        $comment=$comment_authArr['pubcontent'];
        $comment_date=date('Y-m-d H:i:s', time());
        $data = array('comment_post_ID'=>$articleID, 'comment_author'=>$comment_author, 'user_id'=>$userID, 'comment_author_IP'=>$login_ip, 'comment_author_email'=>$user_email, 'comment_content'=>$comment, 'comment_date'=>$comment_date);
        $iden = $this->db->insert($this->tab_com, $data);
        return $iden;
   }
   
    /*
   *评论信息录入
   */
   public function insert_towcomment($towcommentInfo)
   {
        $towcommentArr = json_decode($towcommentInfo, true);
        $articleID=$towcommentArr['articleid'];
        $comment_author=$towcommentArr['username'];
        $userID=$towcommentArr['userid'];
        $login_ip=$towcommentArr['userip'];
        $user_email=$towcommentArr['useremail'];
        $comment=$towcommentArr['content'];
        $fathercommentID=$towcommentArr['fathercommentID'];
        $comment_date=date('Y-m-d H:i:s', time());
        $data = array('comment_author'=>$comment_author, 'user_id'=>$userID, 'comment_author_IP'=>$login_ip, 'comment_author_email'=>$user_email, 'comment_content'=>$comment, 'comment_date'=>$comment_date, 'comment_parent'=>$fathercommentID);
        $iden = $this->db->insert($this->tab_com, $data);
        return $iden;
        
   }
   /*
   *删除评论
   */
   public function del_t_comment($delID)
   {
        $this->db->where('comment_ID',$delID);
        $iden = $this->db->delete($this->tab_com);
        return $iden;
   }
   /*
   *删除评论
   */
   public function del_p_comment($delID)
   {
        $this->db->where('comment_ID',$delID);
        $this->db->or_where('comment_parent',$delID);
        $iden = $this->db->delete($this->tab_com);
        return $iden;
   }
   
   
    /*
    *产品列表
    
    
    public function get_pro()
    {
        $result = $this->db->get($this->tab_one)->result_array();
        return $result;
    }
    
    /*
    *得到产品详细信息

    public function get_pro_content($id)
    {
        
        $this->db->where('id', $id);
        $re = $this->db->get($this->tab_one)->row_array();
        return $re;
    }
    
    /*
    *得到其它产品详细信息
  
    
    public function get_otherpro_content($id)
    {
        $re=$this->db->select('id, proname, prodesc, proimg')->from($this->tab_one)->where('id !=', $id)->get()->result_array();
        
        return $re;
    }
    
    /*
    *得到关于他们列表信息
  
    public function get_with_list($type)
    {
        $resu = $this->db->get($this->tab_tow)->result_array();
        if($type == "more"){
            return $resu;
        }else{
           return array_slice($resu, 0, 5);
               }
    }
    
    public function get_rowOther($id)
    {
        $this->db->where('id', $id);
        $resu = $this->db->get($this->tab_tow)->row_array();
        return $resu;
    }
    
*/   
    
    
}